<?php

namespace backend\modules\common\controllers;

use Yii;
use common\helpers\FileHelper;
use backend\controllers\BaseController;
use yiiframe\auth\Auth;
/**
 * Class SystemController
 * @package backend\modules\base\controllers
 * @author YiiFrame <21931118@qq.com>
 */
class SystemController extends BaseController
{

    /**
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionInfo()
    {
        // 禁用函数
        $disableFunctions = ini_get('disable_functions');
        $disableFunctions = !empty($disableFunctions) ? explode(',', $disableFunctions) : '未禁用';
        // 附件大小
        $attachmentSize = FileHelper::getDirSize(Yii::getAlias('@attachment'));
        $updateinfo = Auth::Version();
        $domain_time = Auth::Authtime();
        return $this->render('info', [
            'mysql_size' => Yii::$app->services->backend->getDefaultDbSize(),
            'attachment_size' => $attachmentSize ?? 0,
            'disable_functions' => $disableFunctions,
            'updateinfo' => $updateinfo,
            'domain_time' => $domain_time,
        ]);
    }
    public function actionUpdate()
    {
        $ver = Yii::$app->debris->version()['version'];
        $hosturl = $_SERVER['HTTP_HOST'];
        $updatehost = 'http://'.Auth::$host.'/api/';
        $updatehosturl = $updatehost . '?a=update&v=' . $ver . '&u=' . $hosturl;
        $updatenowinfo = file_get_contents($updatehosturl);

        if (strstr($updatenowinfo, 'zip')){
            $pathurl = $updatehost . '?a=down&f=' . $updatenowinfo;
            $updatedir = Yii::getAlias('@attachment');
            if($this->get_file($pathurl, $updatenowinfo, $updatedir))
                return $this->message('升级成功，升级包在'.$updatedir, $this->redirect(['info']));
            else return $this->message('升级文件不存在', $this->redirect(['info']));
        }
        else return $this->message($hosturl, $this->redirect(['info']));

    }
    public function delDirAndFile($dirName)
    {
        if ($handle = opendir("$dirName")) {
            while (false !== ($item = readdir($handle))) {
                if ($item != "." && $item != "..") {
                    if (is_dir("$dirName/$item")) {
                        Self::delDirAndFile("$dirName/$item");
                    } else {
                        unlink("$dirName/$item");
                    }
                }
            }
            closedir($handle);
            //rmdir($dirName);
        }
    }

    public function get_file($url,$name,$folder = './')
    {
        set_time_limit((24 * 60) * 60);
        // 设置超时时间
        $destination_folder = $folder . '/';
        // 文件下载保存目录，默认为当前文件目录
        if (!is_dir($destination_folder)) {
            // 判断目录是否存在
            $this->mkdirs($destination_folder);
        }
        $newfname = $destination_folder.$name;
        // 取得文件的名称
        $file = fopen($url, 'rb');
        // 远程下载文件，二进制模式
        if ($file) {
            // 如果下载成功
            $newf = fopen($newfname, 'wb');
            // 远在文件文件
            if ($newf) {
                // 如果文件保存成功
                while (!feof($file)) {
                    // 判断附件写入是否完整
                    fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
                }
            }
        }
        if ($file) {
            fclose($file);
        }
        if ($newf) {
            fclose($newf);
        }
        return true;
    }
    public function mkdirs($path, $mode = '0777')
    {
        if (!is_dir($path)) {
            // 判断目录是否存在
            Self::mkdirs(dirname($path), $mode);
            // 循环建立目录
            mkdir($path, $mode);
        }
        return true;
    }
}